package com.zzyl.nursing.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.constant.HttpStatus;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.nursing.domain.Elder;
import com.zzyl.nursing.dto.ElderDeviceDto;
import com.zzyl.nursing.mapper.ElderMapper;
import com.zzyl.nursing.service.IElderService;
import com.zzyl.nursing.vo.ElderDeviceVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 老人Service业务层处理
 * 
 * @author alexis
 * @date 2025-08-11
 */
@Service
public class ElderServiceImpl extends ServiceImpl<ElderMapper, Elder> implements IElderService
{
    @Autowired
    private ElderMapper elderMapper;

    /**
     * 查询老人
     * 
     * @param id 老人主键
     * @return 老人
     */
    @Override
    public Elder selectElderById(Long id)
    {
        return elderMapper.selectById(id);
    }

    /**
     * 查询老人列表
     * 
     * @param elder 老人
     * @return 老人
     */
    @Override
    public List<Elder> selectElderList(Elder elder)
    {
        return elderMapper.selectElderList(elder);
    }

    /**
     * 新增老人
     * 
     * @param elder 老人
     * @return 结果
     */
    @Override
    public int insertElder(Elder elder)
    {
        return elderMapper.insert(elder);
    }

    /**
     * 修改老人
     * 
     * @param elder 老人
     * @return 结果
     */
    @Override
    public int updateElder(Elder elder)
    {
        return elderMapper.updateById(elder);
    }

    /**
     * 批量删除老人
     * 
     * @param ids 需要删除的老人主键
     * @return 结果
     */
    @Override
    public int deleteElderByIds(Long[] ids)
    {
        return elderMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除老人信息
     * 
     * @param id 老人主键
     * @return 结果
     */
    @Override
    public int deleteElderById(Long id)
    {
        return elderMapper.deleteById(id);
    }

    /**
     * 查询已经入住的老人列表
     * @param status
     * @param pageNum
     * @param pageSize
     * @return
     *//*
    @Override
    public TableDataInfo pageQuery(Integer status, Integer pageNum, Integer pageSize) {
        //1. 分页查询老人数据
        //2.构造条件
        LambdaQueryWrapper<Elder> elderWrapper = new LambdaQueryWrapper<>();
        //3.构建分页条件
        Page<Elder> page = new Page<>(pageNum,pageSize);
        //4.校验状态
        if(ObjUtil.isNotNull(status)){
            elderWrapper.eq(Elder::getStatus,status)
        }
        //sql返回结果，只展示主键、名字、身份照号、床位编号
        elderWrapper.select(Elder::getId, Elder::getName, Elder::getIdCardNo, Elder::getBedNumber);

        //执行查询
        page = page(page, elderWrapper);

        //返回分页结果
        return getDataTable(page);
    }
    *//**
     * 封装分页结果
     * @param page
     * @return
     *//*
    private TableDataInfo getDataTable(Page<Elder> page) {
        //封装分页结果
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setTotal(page.getTotal());
        tableDataInfo.setCode(HttpStatus.SUCCESS);
        tableDataInfo.setMsg("请求成功");
        tableDataInfo.setRows(page.getRecords());
        return tableDataInfo;
    }*/

    /**
     * 老人分页查询
     * @param elderDeviceDto
     * @return
     */
    @Override
    public TableDataInfo<ElderDeviceVo> pageQuery(ElderDeviceDto elderDeviceDto) {
        Page<Elder> page = Page.of(elderDeviceDto.getPageNum(), elderDeviceDto.getPageSize());
        this.lambdaQuery().eq(Elder::getStatus,elderDeviceDto.getStatus())
                .eq(StrUtil.isNotBlank(elderDeviceDto.getIdCardNo()), Elder::getIdCardNo,elderDeviceDto.getIdCardNo())
                .like(StrUtil.isNotBlank(elderDeviceDto.getName()), Elder::getName,elderDeviceDto.getName())
                .page(page);
        List<ElderDeviceVo> elderDeviceVos = BeanUtil.copyToList(page.getRecords(), ElderDeviceVo.class);
        TableDataInfo<ElderDeviceVo> tableDataInfo = new TableDataInfo<>();
        tableDataInfo.setTotal(page.getTotal());
        tableDataInfo.setRows(elderDeviceVos);
        tableDataInfo.setCode(HttpStatus.SUCCESS);
        tableDataInfo.setMsg("查询成功");

        return tableDataInfo;
    }

}
